//
//  MZMessageDetailedViewController.m
//  minzuwenhuagong
//
//  Created by WANG CUNRUI on 14-5-23.
//  Copyright (c) 2014年 WANG CUNRUI. All rights reserved.
//

#import "MZMessageDetailedViewController.h"

//声明常量


@interface MZMessageDetailedViewController ()
@property UIButton *zhanpin;
@property UIButton *weizhi;
@end
@implementation MZMessageDetailedViewController

@synthesize scrollView = _scrollView;
@synthesize detailText = _detailText;
@synthesize messageTitle = _messageTitle;
@synthesize messageTime = _messageTime;
@synthesize messageDept = _messageDept;
@synthesize messageContent = _messageContent;
@synthesize messagePic = _messagePic;
@synthesize image1 = image1;
//@synthesize labelsize = _labelsize;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor cyanColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"简讯详情";
    CGRect frame = self.view.bounds;
    CGSize f = frame.size;
    CGFloat h = f.height;
    CGFloat w = f.width;
    _scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,w,h-115)];
//    _scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,350,456)];

    _scrollView.backgroundColor=[UIColor whiteColor];
    _scrollView.pagingEnabled=YES;
    [self.view addSubview:_scrollView];
    
    _label1 = [[UILabel alloc] initWithFrame:CGRectMake( 10, 5, 300, 30)];
    _label1.layer.cornerRadius = 4;
    _label1.text = _messageTitle;
    _label1.font = [UIFont boldSystemFontOfSize:20];
    //_label1
    [_scrollView addSubview:_label1];
    
    _label2 = [[UILabel alloc] initWithFrame:CGRectMake( 10, 40, 300, 18)];
    _label2.layer.cornerRadius = 4;
    NSString *time = @"开放时间 : ";
    _label2.text = [NSString stringWithFormat:@"%@%@",time,_messageTime];
    _label2.font = [UIFont boldSystemFontOfSize:15];
    [_scrollView addSubview:_label2];
    _label2 = [[UILabel alloc] initWithFrame:CGRectMake( 10, 60, 300, 18)];
    _label2.layer.cornerRadius = 4;
    NSString *dept = @"主办单位 : ";
    _label2.text = [NSString stringWithFormat:@"%@%@",dept,_messageDept];
    _label2.font = [UIFont boldSystemFontOfSize:15];
    [_scrollView addSubview:_label2];
    
    _label3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 250, 0, 0)];
    [_label3 setNumberOfLines:0];
    _label3.lineBreakMode = NSLineBreakByWordWrapping;
    _label3.font = [UIFont boldSystemFontOfSize:15];
    // 测试字串
    NSString *s = _messageContent;
//    UIFont *font = [UIFont fontWithName:@"Arial" size:20];
    _label3.text = s;
    NSLog(@"%@",s);
    //设置一个行高上限
    CGSize size = CGSizeMake(320,400);
    //计算实际frame大小，并将label的frame变成实际大小
    NSDictionary *attribute = @{NSFontAttributeName: _label3.font};
    CGSize labelsize = [_label3.text boundingRectWithSize:size options: NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    _label3.frame = CGRectMake(0,300, labelsize.width, labelsize.height);

    int y = CGRectGetMaxY(_label3.frame);
    [_scrollView setContentSize:(CGSizeMake(CGRectGetWidth(_scrollView.frame), y))];
    
    [_scrollView addSubview:_label3];
    [self loadimage];
     _zhanpin = [UIButton buttonWithType:UIButtonTypeCustom];
    _zhanpin.frame = CGRectMake(35, 260, 80, 30);
    _zhanpin.layer.cornerRadius = 4;
    [_zhanpin setTitle:@"展品" forState:UIControlStateNormal];
    [_zhanpin setBackgroundColor:[UIColor colorWithRed:66/255.0 green:186/255.0 blue:21/255.0 alpha:1.0]];
    [_zhanpin addTarget:self action:@selector(bbiClick1:) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:_zhanpin];
    _weizhi = [UIButton buttonWithType:UIButtonTypeCustom];
    _weizhi.frame = CGRectMake(200, 260, 80, 30);
    _weizhi.layer.cornerRadius = 4;
    [_weizhi setTitle:@"位置" forState:UIControlStateNormal];
    [_weizhi setBackgroundColor:[UIColor colorWithRed:66/255.0 green:186/255.0 blue:21/255.0 alpha:1.0]];
    [_weizhi addTarget:self action:@selector(bbiClick2:) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:_weizhi];
    
}

-(void)loadimage
{
    NSString *imagePath = [NSString stringWithFormat:@"%@", _messagePic];
    NSLog(@"%@",_messagePic);
    NSURL *imageUrl = [NSURL URLWithString:imagePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:imageUrl];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        self.image1 = [UIImage imageWithData:data];
        if (self.image1 == nil)
        {
            // 使用默认图像占位，既能够保证有图像，又能够保证有地方！
            UIImageView *moImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"placeholder.png"]];
            moImage.frame = CGRectMake(0, 80, 320,170);
            [_scrollView addSubview:moImage];
//            [self loadImageAsyncWithIndexPath];
        }
        else
        {
            UIImageView *moImage = [[UIImageView alloc] initWithImage: self.image1];
            moImage.frame = CGRectMake(0, 80, 320,170);
            [_scrollView addSubview:moImage];
        }
    }];

}

- (void)bbiClick1:(UIButton *)bbi
{
    _mzmoreViewController = [[MZmoreViewController alloc]init];
    [self.navigationController pushViewController:_mzmoreViewController animated:YES];
    _mzmoreViewController.MZID = _messageID;
    
}

- (void)bbiClick2:(UIButton *)bbi
{
    _weizhiViewController = [[MZweizhi2ViewController alloc]init];
    [self.navigationController pushViewController:_weizhiViewController animated:YES];
    _weizhiViewController.num = _messageTitle;
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
